$(function () {
    var queryUrl = '../toolsscrap/list';
    var Id = getQueryString('Id');
    if(Id){
        queryUrl += '?Id=' + Id;
    }
    $("#jqGrid").Grid({
        url: queryUrl,
        colModel: [
            {label: '唯一编号', name: 'onlyCode', index: 'ONLY_CODE', width: 80},
            {
                label: '处置日期', name: 'dealDate', index: 'DEAL_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '名称', name: 'name', index: 'NAME', width: 80},
            {label: '型号规格', name: 'spec', index: 'SPEC', width: 80},
            {name: 'onlyMark', hidden: true},
            {label: '图号', name: 'mapNo', index: 'MAP_NO', width: 80},
            {label: '自制图号', name: 'createNo', index: 'createNo', width: 80},
            {label: '工装类别', name: 'toolsTypeName', index: 'toolsTypeName', width: 80},
            {label: '所属车间名称', name: 'farmName', index: 'FARM_NAME', width: 80},
            {label: '使用工序号', name: 'workProCode', index: 'WORK_PRO_CODE', width: 80},
            {label: '报废数', name: 'scrapNum', index: 'SCRAP_NUM', width: 80},
            {label: '现象描述', name: 'descrCon', index: 'DESCR_CON', width: 80},
            {label: '判定与建议', name: 'advice', index: 'ADVICE', width: 80},
            {
                label: '审批状态',hidden: true, name: 'approvalStatus', index: 'APPROVAL_STATUS', width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },
            {
                label: '审批时间',hidden: true, name: 'approvalDate', index: 'APPROVAL_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '制造处审批人',hidden: true, name: 'peName', index: 'PE_APPROVAL', width: 80},
            {
                label: '审批状态',hidden: true, name: 'peApprovalStatus', index: 'PE_APPROVAL_STATUS', width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },
            {
                label: '审批时间',hidden: true, name: 'peApprovalDate', index: 'PE_APPROVAL_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            {
                label: '审批详情', name: 'devCode', index: 'DEV_CODE', width: 80,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.shviewOption(\"" + escape(JSON.stringify(row)) + "\")'>&nbsp;详情</button>";
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {
                label: 'status', name: 'status', index: 'status', hidden: true,
                formatter: function (value, col, row) {
                    return row.approvalStatus;
                }
            },
            {
                label: 'peStatus', name: 'peStatus', index: 'peStatus', hidden: true,
                formatter: function (value, col, row) {
                    return row.peApprovalStatus;
                }
            },
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        toolsScrap: {
            delFlag: '',
            remark: '',
            peApproval: '',
            qualithApproval: '',
            apply: '',
            advice: '',
            descrCon: '',
            scrapNum: '',
            workProName: '',
            workProCode: '',
            prodName: '',
            prodCode: '',
            toolsType: '',
            spec: '',
            name: '',
            farmName: '',
            farmId: '',
            toolsVersion: '',
            onlyCode: '',
            id: '',
            approvalDate: '',
            approvalStatus: '',
            mapNo: '',
            onlyMark: '',
            dealDate: new Date(),
            peApprovalDate: '',
            peApprovalStatus: '',
            checkResult: '',
            checkResult2:''
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'change'}
            ]
        },
        q: {
            name: ''
        },
        toolsTypeList:[],
        checkResult: 1,
        checkResult2: 1
    },
    methods: {
        shviewOption: function (res) {
            let data = JSON.parse(unescape(res))
            if(!data.qualithName){data.qualithName="-"}
            if(!data.peName){data.peName="-"}
            let dom = "<div class='details' >" +
                "<table>" +
                "<tr><td>技术质量室审批人：</td><td>"+data.qualithName+"</td></tr>" +
                "<tr><td>审批状态：</td><td>"+sysEnums.CHECK_FLAG[data.approvalStatus]+"</td></tr>" +
                "<tr><td>审批时间：</td><td>"+transDate(data.approvalDate, "yyyy-MM-dd")+"</td></tr>" +
                "<tr><td>制造处审批人：</td><td>"+data.peName+"</td></tr>" +
                "<tr><td>审批状态：</td><td>"+sysEnums.CHECK_FLAG[data.peApprovalStatus]+"</td></tr>" +
                "<tr><td>审批时间：</td><td>"+transDate(data.peApprovalDate, "yyyy-MM-dd")+"</td></tr>" +
                "</table></div>"
            layer.open({
                type: 1,
                title: "详情",
                closeBtn: 0,
                area: ['450px'],
                offset: '10vh',
                shadeClose: true,
                skin: 'yourclass',
                content: dom
            });
        },
        check: function () {
            var data = getSelectedRowData("#jqGrid");

            if (data.id == null) {
                return;
            }

            if (data.status !== '0') {
                alert("此条记录已审核");
                return;
            }
            openWindow({
                title: "确认审核",
                area: ['400px', '240px'],
                content: jQuery("#reviewLayer1"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    vm.toolsScrap = {
                        approvalStatus: vm.checkResult,
                        id: data.id,
                        advice: vm.toolsScrap.advice
                    };
                    var url = '../toolsscrap/check';
                    var params = JSON.stringify(vm.toolsScrap);
                    vm.updateCheck(url, params)
                }
            });

        },
        getToolsType: function () {
            Ajax.request({
                url: "../toolstype/queryAll",
                // params: {typeTp: 2},
                async: true,
                successCallback: function (r) {
                    vm.toolsTypeList = r.list;
                }
            });
        },
        peCheck: function () {
            var data = getSelectedRowData("#jqGrid");

            if (data.id == null) {
                return;
            }

            if (data.peStatus !== '0') {
                alert("此条记录已审核");
                return;
            }

            if (data.status !== '1') {
                alert("此条记录未通过技术质量室审核");
                return;
            }
            openWindow({
                title: "确认审核",
                area: ['300px', '200px'],
                content: jQuery("#reviewLayer2"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    vm.toolsScrap = {peApprovalStatus: vm.checkResult2, id: data.id};
                    var url = '../toolsscrap/peCheck';
                    var params = JSON.stringify(vm.toolsScrap);
                    vm.updateCheck(url, params)
                }
            });

        },
        updateCheck: function (url, params) {
            Ajax.request({
                url: url,
                params: params,
                contentType: "application/json",
                type: 'POST',
                successCallback: function (res) {
                    alert('审核成功!');
                    setTimeout(function () {
                        location.reload();
                    }, 1000);
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.toolsScrap = {
                delFlag: '',
                remark: '',
                peApproval: '',
                qualithApproval: '',
                apply: '',
                advice: '',
                descrCon: '',
                scrapNum: '',
                workProName: '',
                workProCode: '',
                prodName: '',
                prodCode: '',
                toolsType: '',
                spec: '',
                name: '',
                farmName: '',
                farmId: '',
                toolsVersion: '',
                onlyCode: '',
                approvalDate: '',
                approvalStatus: '',
                mapNo: '',
                onlyMark: '',
                storePos: '',
                dealDate: '',
                peApprovalDate: '',
                peApprovalStatus: '',
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }

            var data = getSelectedRowData("#jqGrid");

            var flag  = data.approvalStatus;
            if(flag=='已审核'){
                alert('当前进入流程禁止修改删除！')
                return ;
            }


            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.toolsScrap.id == null ? "../toolsscrap/save" : "../toolsscrap/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.toolsScrap),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../toolsscrap/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../toolsscrap/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.toolsScrap = r.toolsScrap;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getToolsInfo:function(){
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 工装清单',
                content: '../tools/toolslistSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getTools();
                    if (!res) {
                        return;
                    }
                    /*唯一编码*/
                    vm.toolsScrap.onlyMark = res.toolsCode;
                    vm.toolsScrap.name = res.name;
                    vm.toolsScrap.spec = res.type;
                    vm.toolsScrap.mapNo = res.mapNo;
                    vm.toolsScrap.deptCode = res.deptCode;
                    vm.toolsScrap.storePos = res.id;
                    vm.toolsScrap.workProCode = res.useProcedure;
                    layer.close(index);
                }
            })
        },
    }
});
